package cn.enjoy.design.structure.zuhe.logic;


import cn.enjoy.design.structure.zuhe.entity.Node;

import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 */
public abstract class BaseLogic implements LogicFilter {

    @Override
    public Node filter(String matterValue, List<Node> nodeList) {
        for (Node node : nodeList) {
            if (decisionLogic(matterValue, node)) {
                return node;
            }
        }
        return null;
    }

    /**
     * 获取决策值
     *
     * @param treeId         规则树ID
     * @param userId         用户ID
     * @param decisionMatter 决策物料
     * @return 决策值
     */
    @Override
    public abstract String matterValue(Long treeId, String userId, Map<String, String> decisionMatter);

    /**
     * 定义了基本的决策⽅法； 1、2、3、4、5 ， 等于、⼩于、⼤于、⼩于等于、⼤于等于 的判断逻辑。
     *
     * @param matterValue 匹配值
     * @param node        节点链接
     * @return boolean
     */
    private boolean decisionLogic(String matterValue, Node node) {
        switch (node.getRuleLimitType()) {
            case 1:
                return matterValue.equals(node.getRuleLimitValue());
            case 2:
                return Double.parseDouble(matterValue) > Double.parseDouble(node.getRuleLimitValue());
            case 3:
                return Double.parseDouble(matterValue) < Double.parseDouble(node.getRuleLimitValue());
            case 4:
                return Double.parseDouble(matterValue) <= Double.parseDouble(node.getRuleLimitValue());
            case 5:
                return Double.parseDouble(matterValue) >= Double.parseDouble(node.getRuleLimitValue());
            default:
                return false;
        }
    }

}
